<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by an Apache 2.0 License.
See the COPYING file for details.
-->
<head>
<title>goog.dom.TextRangeIterator Tests</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.dom.TagName');
  goog.require('goog.dom.TextRangeIterator');
  goog.require('goog.testing.dom');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>

<!--
  The next line goes past 80 characters to avoid ambiguity with
  newlines as text nodes
-->
<div id="test"><a href="#" id="a1">T<b id="b1">e</b>xt</a><span id="span1"></span><p id="p1">Text</p></div>
<ul id="test2">foo<br>bar</ul>

<script>
  var test = goog.dom.getElement('test');
  var test2 = goog.dom.getElement('test2');

  function testBasic() {
    goog.testing.dom.assertNodesMatch(
        new goog.dom.TextRangeIterator(test, 0, test, 2),
        ['#a1', 'T', '#b1', 'e', '#b1', 'xt', '#a1', '#span1',
          '#span1', '#p1']);
  }

  function testAdjustStart() {
    var iterator = new goog.dom.TextRangeIterator(test, 0, test, 2);
    iterator.setStartNode(goog.dom.getElement('span1'));

    goog.testing.dom.assertNodesMatch(iterator,
        ['#span1', '#span1', '#p1']);
  }

  function testAdjustEnd() {
    var iterator = new goog.dom.TextRangeIterator(test, 0, test, 2);
    iterator.setEndNode(goog.dom.getElement('span1'));

    goog.testing.dom.assertNodesMatch(iterator,
        ['#a1', 'T', '#b1', 'e', '#b1', 'xt', '#a1', '#span1']);
  }

  function testOffsets() {
    var iterator = new goog.dom.TextRangeIterator(test2.firstChild, 1,
        test2.lastChild, 2);

    // foo
    var node = iterator.next();
    assertEquals('Should have start offset at iteration step 1', 1,
        iterator.getStartTextOffset());
    assertEquals('Should not have end offset at iteration step 1',
        node.nodeValue.length, iterator.getEndTextOffset());

    // <br>
    node = iterator.next();
    assertEquals('Should not have start offset at iteration step 2', -1,
        iterator.getStartTextOffset());
    assertEquals('Should not have end offset at iteration step 2', -1,
        iterator.getEndTextOffset());

    // </br>
    node = iterator.next();
    assertEquals('Should not have start offset at iteration step 3', -1,
        iterator.getStartTextOffset());
    assertEquals('Should not have end offset at iteration step 3', -1,
        iterator.getEndTextOffset());

    // bar
    node = iterator.next();
    assertEquals('Should not have start offset at iteration step 4', 0,
        iterator.getStartTextOffset());
    assertEquals('Should have end offset at iteration step 4', 2,
        iterator.getEndTextOffset());
  }

  function testSingleNodeOffsets() {
    var iterator = new goog.dom.TextRangeIterator(test2.firstChild, 1,
        test2.firstChild, 2);

    iterator.next();
    assertEquals('Should have start offset', 1, iterator.getStartTextOffset());
    assertEquals('Should have end offset', 2, iterator.getEndTextOffset());
  }

  function testEndNodeOffsetAtEnd() {
    var iterator = new goog.dom.TextRangeIterator(
        goog.dom.getElement('b1').firstChild, 0, goog.dom.getElement('b1'), 1);
    goog.testing.dom.assertNodesMatch(iterator, ['e', '#b1']);
  }

  function testSkipTagDoesNotSkipEnd() {
    // Iterate over 'Tex'.
    var iterator = new goog.dom.TextRangeIterator(
        test.firstChild.firstChild, 0,
        test.firstChild.lastChild, 1);

    var node = iterator.next();
    assertEquals('T', node.nodeValue);

    node = iterator.next();
    assertEquals(goog.dom.TagName.B, node.tagName);

    iterator.skipTag();

    node = iterator.next();
    assertEquals('xt', node.nodeValue);
  }

  function testSkipTagSkipsEnd() {
    // Iterate over 'Te'.
    var iterator = new goog.dom.TextRangeIterator(
        test.firstChild.firstChild, 0,
        test.getElementsByTagName(goog.dom.TagName.B)[0].firstChild, 1);

    var node = iterator.next();
    assertEquals('T', node.nodeValue);

    node = iterator.next();
    assertEquals(goog.dom.TagName.B, node.tagName);

    try {
      iterator.skipTag();
      fail('Should stop iteration when skipping B');
    } catch (ex) {
      assertEquals(goog.iter.StopIteration, ex);
    }
  }
</script>
</body>
</html>
